package manage;

import logIn.MysqlConnectionTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 注册界面实现、缺少判断手机号是否全为数字
 * @Author Jian Chen long
 * @Date 2021/6/15 21:19
 */
public class newInterFace extends JFrame {
    private JPanel panel = new JPanel();
    private JPanel panel1 = new JPanel(new GridLayout(3,1,0,0));

    private JLabel nameLabel = new JLabel("姓名");
    private JTextField nameText = new JTextField(10);

    private JLabel passwordLabel = new JLabel("密码");
    private JPasswordField passwordText = new JPasswordField(18);
//    private JTextField passwordText = new JTextField(18);

    private JLabel phoneLabel = new JLabel("电话");
    private JTextField phoneText = new JTextField(11);

    private JPanel panel2 = new JPanel(new GridLayout(1,1,0,0));
    private JButton newButton= new JButton("入职");
    private JButton cancelButton = new JButton("取消");


    public newInterFace(){
        panel1.add(nameLabel);
        panel1.add(nameText);
        panel1.add(passwordLabel);
        panel1.add(passwordText);
        panel1.add(phoneLabel);
        panel1.add(phoneText);
        panel2.add(newButton);
        panel2.add(cancelButton);
        panel.add(panel1,BorderLayout.NORTH);
        panel.add(panel2,BorderLayout.SOUTH);
        this.add(panel);

        this.setTitle("注册功能");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(450,180);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setResizable(true);

        /**
         * 注册功能实现
         */
        newButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 得到姓名、注册密码、注册手机号
                String name = nameText.getText();
                String password = passwordText.getText();
                String phone = phoneText.getText();
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                try{
                    // 没有填写完整的信息
                    if (name.length() == 0 || password.length() == 0 || phone.length() == 0){
                        JOptionPane.showMessageDialog(null,"信息不完整");
                        return;
                    }else if(password.length() > 18){
                        // 判断密码是否大于18位
                        JOptionPane.showMessageDialog(null,"密码不超过18位");
                        return;
                    }else if (phone.length() > 12){
                        // 这里缺少判断手机号是否全为数字
                        JOptionPane.showMessageDialog(null,"手机号错误");
                        return;
                    }else {
                        // 管理员登陆名为admin,判断注册用户姓名是否与admin重复,若重复重新输入
                        if (name.equals("admin") == true){
                            JOptionPane.showMessageDialog(null,"姓名错误,请重新输入");
                            return;
                        }
                        conn = MysqlConnectionTool.getConnection();
                        // 获取预编译数据库操作对象
                        String sql = "insert into users(username,userPassword,userphone) values(?,?,?)";
                        //程序执行到这，或发送sql语句框子给DBMS，然后DBMS进行sql语句的预先编译
                        ps =  conn.prepareStatement(sql);
                        //给占位符 ? 占位符传值(第一个问好下标为1，第二个问号下标为2，JDBC中所有的下标从1开始)
                        ps.setString(1,name);
                        ps.setString(2,password);
                        ps.setString(3,phone);
                        ps.executeUpdate();
                        // 告诉用户注册成功,获取登陆编号,下次登陆,不可修改
                        // 使用视图,增加数据的安全性
                        String sql2= "select 编号 from userview where 联系方式 = " + phone;
                        ps =  conn.prepareStatement(sql2);
                        rs = ps.executeQuery();
                        while (rs.next()){
                            JOptionPane.showMessageDialog(null,"注册成功,工号为:" + rs.getString("编号") + ",请关闭注册页");
                            // 注册成功后关闭子窗口
                            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                            nameText.setText(null);
                            passwordText.setText(null);
                            phoneText.setText(null);
                        }
                    }
                }catch (Exception E){
                    JOptionPane.showMessageDialog(null,"输入正确的手机号");
                    nameText.setText(null);
                    passwordText.setText(null);
                    phoneText.setText(null);
                    return;
                } finally {
                    MysqlConnectionTool.close(conn,ps,rs);
                }
            }
        });


        /**
         * 取消按钮实现,关闭程序
         */
        cancelButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"系统关闭");
                System.exit(0);
            }
        });
    }
}
